package com.ruoyi.wm.service.impl;

import com.ruoyi.wm.domain.WmApparatusInventory;
import com.ruoyi.wm.domain.WmMedicineInventory;
import com.ruoyi.wm.mapper.WmApparatusInventoryMapper;
import com.ruoyi.wm.mapper.WmApparatusOutstorageListMapper;
import com.ruoyi.wm.mapper.WmMedicineInventoryMapper;
import com.ruoyi.wm.mapper.WmMedicineOutstorageListMapper;
import com.ruoyi.wm.service.ITimeTaskService;
import com.ruoyi.wm.vo.WmApparatusInventoryVo;
import com.ruoyi.wm.vo.WmMedicineInventoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;

@Service
public class TimeTaskService implements ITimeTaskService {
    @Autowired
    private WmApparatusOutstorageListMapper wmApparatusOutstorageListMapper;
    @Autowired
    private WmMedicineOutstorageListMapper wmMedicineOutstorageListMapper;

    @Autowired
    private WmMedicineInventoryMapper wmMedicineInventoryMapper;
    @Autowired
    private WmApparatusInventoryMapper wmApparatusInventoryMapper;

    @Override
    public  int addInventoryPerMonth() {
        //获取当前的药品和器材的库存信息
        ArrayList<WmApparatusInventoryVo> apparatusList = (ArrayList<WmApparatusInventoryVo>) wmApparatusOutstorageListMapper.selectInventoryVo();
        ArrayList<WmMedicineInventoryVo> medicineList = (ArrayList<WmMedicineInventoryVo>) wmMedicineOutstorageListMapper.selectInventoryVo();
        for (WmApparatusInventoryVo apparatusInventory :apparatusList) {
            WmApparatusInventory appartus = new WmApparatusInventory();
            appartus.setName(apparatusInventory.getName());
            appartus.setForm(apparatusInventory.getStandard());
            appartus.setTime(new Date());
            long outNum = apparatusInventory.getOutNum();
            appartus.setOutStorageNum(outNum);
            BigDecimal totalPrice = apparatusInventory.getTotalPrice();
            BigDecimal divide = totalPrice.divide(BigDecimal.valueOf(outNum),3,RoundingMode.HALF_UP);
            appartus.setOutPrice(divide);
            appartus.setOutTotalPrice(apparatusInventory.getTotalPrice());
            WmApparatusInventory inventory = wmApparatusInventoryMapper.selectLatestIndex(apparatusInventory.getName());
            long currentNum = inventory.getRemainNum() - apparatusInventory.getOutNum();
            appartus.setRemainNum(currentNum);
            BigDecimal currentTotalPrice = inventory.getRemainTotalPrice().min(apparatusInventory.getTotalPrice());
            appartus.setRemainTotalPrice(currentTotalPrice);
            appartus.setRemianPrice(currentTotalPrice.divide(BigDecimal.valueOf(currentNum),3, RoundingMode.HALF_UP));
            wmApparatusInventoryMapper.insertWmApparatusInventory(appartus);
        }
        for (WmMedicineInventoryVo medicineInventory :medicineList) {
            WmMedicineInventory medicine = new WmMedicineInventory();
            medicine.setName(medicineInventory.getName());
            medicine.setForm(medicineInventory.getStandard());
            medicine.setTime(new Date());
            long outNum = medicineInventory.getOutNum();
            medicine.setOutStorageNum(outNum);
            medicine.setOutTotalPrice(medicineInventory.getTotalPrice());
            BigDecimal totalPrice = medicineInventory.getTotalPrice();
            BigDecimal divide = totalPrice.divide(BigDecimal.valueOf(outNum),3,RoundingMode.HALF_UP);
            medicine.setOutPrice(divide);
            medicine.setOutTotalPrice(medicineInventory.getTotalPrice());
            WmMedicineInventory inventory = wmMedicineInventoryMapper.selectLatestIndex(medicineInventory.getName());
            long currentNum = inventory.getRemainNum() - medicineInventory.getOutNum();
            medicine.setRemainNum(currentNum);
            BigDecimal currentTotalPrice = inventory.getRemainTotalPrice().min(medicineInventory.getTotalPrice());
            medicine.setRemainTotalPrice(currentTotalPrice);
            medicine.setRemianPrice(currentTotalPrice.divide(BigDecimal.valueOf(currentNum),3, RoundingMode.HALF_UP));
            wmMedicineInventoryMapper.insertWmMedicineInventory(medicine);
        }
        return 1;
    }
}
